import { getList } from '../../api/products'

export default {
  namespaced: true,
  state: {
    totals: 0,
    pages: 1,
    page: 1,
    list: [],
    isLoading: false
  },
  mutations: {
    loadDataEnd(state, payload) {
      state.totals = payload.totalCount
      state.pages = payload.pages
      state.list = payload.products
      state.isLoading = false
    },
    resetList(state) {
      state.isLoading = true
      state.list = []
    }
  },
  actions: {
    async loadData({ commit }, payload) {
      commit('resetList')
      const res = await getList(payload)
      commit('loadDataEnd', res)
    }
  }
}
